﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyData.Common;
using System.Windows.Forms;
using System.Threading;
using System.Activities;
using System.Activities.XamlIntegration;
using System.Activities.Tracking;

namespace WorkflowDesigner
{
  class SampleActivity : IClientActivity
  {
    private WorkflowActivitiesControl _wfActivityContol;

    public SampleActivity(WorkflowActivitiesControl wfActivityContol)
    {
      _wfActivityContol = wfActivityContol;
    }

    public void Run(string workflowPath)
    {
      Cursor.Current = Cursors.WaitCursor;

      AutoResetEvent syncEvent = new AutoResetEvent(false);

      var inputs = new Dictionary<String, Object>
        {
          {"ArgTitle",  "Test Title"},
          {"ArgBody",  "Test Body"},
          {"Handle", new IntPtr(0)}
        };

      Activity wf = (Activity)ActivityXamlServices.Load(workflowPath);

      WorkflowApplication wfApp = new WorkflowApplication(wf, inputs);

      Tracking sampleParticipant = new Tracking
      {
        TrackingProfile = new TrackingProfile()
        {
          Name = "CustomTrackingProfile",
          Queries = 
        {
          new ActivityStateQuery()
          {
              // Subscribe for track records from all activities for all states
              ActivityName = "*",
              States = { ActivityStates.Closed},

              // Extract workflow variables and arguments as a part of the activity tracking record
              // Variables = "*" allows for extraction of all variables in the scope of the activity
              // Arguments = "*" allows for extraction of all arguments in the scope of the activity
              Variables = 
              {                                
                  { "*" }
              },
              Arguments = 
              {
                  { "*" }
              }
          }   
        }
        }

      };


      sampleParticipant.ActivityStateTracked += new Tracking.ActivityStateTrackedHandler(sampleParticipant_ActivityStateTracked);
      wfApp.Extensions.Add(sampleParticipant);

      wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs wfe)
      {
        IDictionary<String, Object> output = wfe.Outputs;

        syncEvent.Set();
      };

      wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs wfe)
      {
        syncEvent.Set();
      };

      wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs efee)
      {
        return UnhandledExceptionAction.Terminate;
      };

      _wfActivityContol.objectsTreeView.Nodes.Clear();

      wfApp.Run();

      Cursor.Current = Cursors.Default;
    }

    void sampleParticipant_ActivityStateTracked(object sender, ActivityStateTrackedEventArgs e)
    {
      _wfActivityContol.Invoke(new WorkflowActivitiesControl.AddNodeDelegate
        (_wfActivityContol.AddNode), e.ActivityStateRecord.Activity.Name, e.ActivityStateRecord.Arguments);    
    }
  }
}
